package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.domain.SysCheck;
import com.ruoyi.system.domain.SysMeet;
import com.ruoyi.system.mapper.SysCheckMapper;
import com.ruoyi.system.mapper.SysMeetMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.bo.SysProjectBo;
import com.ruoyi.system.domain.vo.SysProjectVo;
import com.ruoyi.system.domain.SysProject;
import com.ruoyi.system.mapper.SysProjectMapper;
import com.ruoyi.system.service.ISysProjectService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 项目Service业务层处理
 *
 * @author ruoyi
 * @date 2022-04-17
 */
@RequiredArgsConstructor
@Service
public class SysProjectServiceImpl implements ISysProjectService {

    private final SysProjectMapper baseMapper;

    @Autowired
    private SysCheckMapper checkMapper;

    /**
     * 查询项目
     *
     * @param id 项目主键
     * @return 项目
     */
    @Override
    public SysProjectVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询项目列表
     *
     * @param bo 项目
     * @return 项目
     */
    @Override
    public TableDataInfo<SysProjectVo> queryPageList(SysProjectBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysProject> lqw = buildQueryWrapper(bo);
        Page<SysProjectVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询项目列表
     *
     * @param bo 项目
     * @return 项目
     */
    @Override
    public List<SysProjectVo> queryList(SysProjectBo bo) {
        LambdaQueryWrapper<SysProject> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysProject> buildQueryWrapper(SysProjectBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysProject> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), SysProject::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), SysProject::getType, bo.getType());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), SysProject::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增项目
     *
     * @param bo 项目
     * @return 结果
     */
    @Override
    public Boolean insertByBo(SysProjectBo bo) {
        SysProject add = BeanUtil.toBean(bo, SysProject.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改项目
     *
     * @param bo 项目
     * @return 结果
     */
    @Override
    public Boolean updateByBo(SysProjectBo bo) {
        SysProject update = BeanUtil.toBean(bo, SysProject.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(SysProject entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除项目
     *
     * @param ids 需要删除的项目主键
     * @return 结果
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public Boolean check(Long id) {
        SysProject sysProject = baseMapper.selectById(id);
        sysProject.setStatus(Constants.SYS_PROJECT_STATUS_DSH);
        baseMapper.updateById(sysProject);
        SysCheck sysCheck = new SysCheck();
        sysCheck.setProjectId(id);
        sysCheck.setStatus(Constants.SYS_CHECK_STATUS_DSH);
        return checkMapper.insert(sysCheck) > 0;
    }
}
